<?php

include_once ("mysqllib.php");

class annotation {

	private $idAnnotation;
	private $idAuthor;
	private $title;
	private $wikiPage;
	private $idVideo;
	private $time;
	private $editable;
	private $shared;

	//adds the annotation to the data base
	public function addAnnotation($idAuthor, $title, $wikiPage, $idVideo, $time, $shared, $editable) {
		//connect to db
		$db = new mysqllib();

		//insert the new annotation in the database
		$query = "INSERT INTO Annotation (idAuthor, title, ewikiPageName, idVideo, time, shared, editable)
				  VALUES ('$idAuthor', '$title', '$wikiPage', '$idVideo', '$time', '$shared', '$editable')";
		$result = mysql_query($query);

		//If the insertion worked
		if ($result) {
			//get the id of the newly created annotation.
			$query2 = "SELECT idAnnotation FROM Annotation where 
					  idAuthor = '$idAuthor' AND title = '$title' AND ewikiPageName = '$wikiPage' AND idVideo='$idVideo'
					  AND time='$time' AND shared='$shared' AND editable='$editable'";

			$result2 = mysql_query($query2);
			$data2 = mysql_fetch_array($result2);
			$idAnnotation = $data2['idAnnotation'];

			//set the private variables
			$this -> idAnnotation = $idAnnotation;
			$this->idAuthor = $idAuthor;
			$this -> title = $title;
			$this -> wikiPage = $wikiPage;
			$this -> idVideo = $idVideo;
			$this -> time = $time;
			$this -> shared = $shared;
			$this -> editable = $editable;
		}
		return $result;
	}

	public function editAnnotation($idPage, $idAuthor, $content) {
		$db = new mysqllib();
		$query = "UPDATE Annotation SET ewikiPageName='$content', idAuthor='$idAuthor' WHERE idAnnotation='$idPage'";
		$result = mysql_query($query);
		if ($result) {
			return true;
		} else {
			return false;
		}
	}

	public function setAnnotation($idAnnotation, $idAuthor, $title, $wikiPage, $idVideo, $time, $shared, $editable) {
		$this -> idAnnotation = $idAnnotation;
		$this -> idAuthor = $idAuthor;
		$this -> title = $title;
		$this -> wikiPage = $wikiPage;
		$this -> idVideo = $idVideo;
		$this -> time = $time;
		$this -> shared = $shared;
		$this -> editable = $editable;
	}

	public function displayAnnotationForList() {
		$db = new mysqllib();
		session_start();
		echo '<h3 id =' . $this -> idAnnotation . '>' . $this -> title . '</h3>';

		echo '<div id =annot' . $this -> idAnnotation . '>' . '<p>'. $this -> wikiPage.'</p>';

		//If the annotation is editable and the user is not blocked, display the edit button.
		if (($this -> editable && !$_SESSION['blocked']) || ($_SESSION['status'] == $GLOBALS['admin'] || $_SESSION['status'] == $GLOBALS['professor']) || $this->shared == 0) {
			echo '<button onclick="editWikiPage(' . $this -> idAnnotation . ',' . "'" . $this -> wikiPage . "'" . ')">Edit this Annotation</button><br/>';
			echo '<button onclick="deleteWikiPage(' . $this -> idAnnotation . ')">Delete this Annotation</button>';
		}
		echo ' </div>';
	}

	public function displayAnnotationPin($duration, $id) {
		echo '<img id="' . $id . '" data-timeannot="' . $this -> time . '" onclick="openAnnotationInList(' . $id . ')" src="img/pinBlack.png" width = 12 height = 20 style="position:absolute; left:';
		$pos = (($this -> time / $duration) - (6/640)) * 100;
		echo "$pos%";
		echo ';">';
	}

}
?>